<?php include("header.php") ?>
<section class="page container">
    <div class="row">
        <div class="span14">
            <h2><a href="<?php nav_url("petrostock") ?>">Solid Petro-Stock</a> / <?php echo $module ?></h2>
        </div>
        <div class="span2">
            <a class="btn" href="<?php nav_url("petrostock") ?>" style="height:35px;"><h5><i
                        class="icon-arrow-left icon-large"></i> Back</h5></a>
        </div>
    </div>
    <hr>
    <form id="addEditPetrostock" class="form-horizontal">
        <div class="container">
            <div class="row">
                <div class="span4">
                    <div class="well">
                        <h4 align="center"> <?php if ($module != 'Edit') {
                                echo "Current ";
                            } ?> Opening Stock</h4>
                        <h4 id="l_OpeningStock" align="center"><?php if (isset($petroStock) && $petroStock != null) {
                                echo $petroStock->getOpeningStock();
                            } ?></h4>
                    </div>
                </div>
                <div class="span8"></div>
                <div class="span4">
                    <div class="well">
                        <h4 align="center"> <?php if ($module != 'Edit') {
                                echo "Current ";
                            } ?> Closing Stock</h4>

                        <h4 id="l_ClosingStock" align="center"><?php if (isset($petroStock) && $petroStock != null) {
                                echo $petroStock->getClosingStock();
                            } ?></h4>
                    </div>
                </div>
            </div>
            <br/><br/>

            <div class="row">
                <div class="span3">
                </div>
                <div class="span13">
                    <fieldset>
                        <div class="control-group ">
                            <input type="hidden" id="id" name="id"
                                   value="<?php if (isset($petroStock) && $petroStock != null) {
                                       echo $petroStock->getId();
                                   } ?>"/>
                            <label class="control-label">Date <span class="required">*</span></label>

                            <div class="controls">
                                <input id="date" name="date" class="datepicker" type="text" data-validation="date"
                                       data-validation-format="dd-mm-yyyy" placeholder="Date"
                                       data-validation-error-msg="Please enter valid Date. ( dd-mm-yyyy )"
                                       value="<?php if (isset($petroStock) && $petroStock != null) {
                                           if ($petroStock->getDate() != null) {
                                               echo $petroStock->getDate()->format('d-m-Y');
                                           }
                                       } ?>">
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label">Grade <span class="required">*</span></label>

                            <div class="controls">
                                <select id="grade" name="grade">
                                    <?php
                                    foreach ($grades as $grade) {
                                        if (isset($petroStock) && $petroStock != null) {
                                            if ($petroStock->getGrade() != null && $petroStock->getGrade()->getId() == $grade->getId()) {
                                                echo "<option selected='selected' value='" . $grade->getId() . "'>" . $grade->getName() . "</option>";
                                            } else {
                                                echo "<option value='" . $grade->getId() . "'>" . $grade->getName() . "</option>";
                                            }
                                        } else {
                                            echo "<option value='" . $grade->getId() . "'>" . $grade->getName() . "</option>";
                                        }
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label">Purchase & Production (Add) <span
                                    class="required">*</span></label>

                            <div class="controls">
                                <input id="purchaseAndProduction" name="purchaseAndProduction" class="span5 number"
                                       type="text" placeholder="Purchase and Production"
                                       data-validation="number" data-validation-error-msg="Please enter valid Number."
                                       value="<?php if (isset($petroStock) && $petroStock != null) {
                                           echo $petroStock->getPurchaseAndProduction();
                                       }?>">
                            </div>
                        </div>
                        <div class="control-group ">
                            <label class="control-label">Sales & Consumption (Less) <span
                                    class="required">*</span></label>

                            <div class="controls">
                                <input id="salesAndConsumption" name="salesAndConsumption" class="span5 number"
                                       type="text" placeholder="Sales and Consumption"
                                       data-validation="number" data-validation-error-msg="Please enter valid Number."
                                       value="<?php if (isset($petroStock) && $petroStock != null) {
                                           echo $petroStock->getSaleAndConsumption();
                                       }?>">
                            </div>
                        </div>
                        <div class="control-group ">
                            <div class="controls">
                                <button id="submit-button" type="submit" class="btn btn-primary ladda-button"
                                        data-style="slide-right">
                                    <span class="ladda-label">Save</span>
                                </button>
                                <button type="reset" class="btn">Cancel</button>
                            </div>
                        </div>
                    </fieldset>
                </div>
            </div>
        </div>
    </form>
    <div class="row">
        <div class="span4"></div>
        <div class="span8">
            <div id="status"></div>
        </div>
        <div class="span4"></div>
    </div>
</section>
<?php include("footer.php") ?>
<script>

    $("#purchaseAndProduction").keyup(function () {
        calculateClosingStockonPurchase();
    });


    $("#salesAndConsumption").keyup(function () {
        calculateClosingStockonSales();
    });


    function calculateClosingStockonPurchase() {
        var closingStock = <?php if (isset($petroStock) && $petroStock != null) { echo $petroStock->getOpeningStock(); }else{ echo 0;} ?>;
        var saleAndConsumption = parseFloat($("#salesAndConsumption").val());
        var value = $("#purchaseAndProduction").val();
        if ($.trim(value).length) {
            if (saleAndConsumption >= 0) {
                $("#l_ClosingStock").text((closingStock + saleAndConsumption) - parseFloat(value));
            } else {
                $("#l_ClosingStock").text(closingStock - parseFloat(value));
            }
        } else {
            if (saleAndConsumption >= 0) {
                $("#l_ClosingStock").text(closingStock + saleAndConsumption);
            } else {
                $("#l_ClosingStock").text(closingStock + 0);
            }

        }
    }

    function calculateClosingStockonSales() {
        var closingStock = <?php if (isset($petroStock) && $petroStock != null) { echo $petroStock->getOpeningStock(); }else{ echo 0;} ?>;
        var purchaseAndProduction = $("#purchaseAndProduction").val();
        var value = $("#salesAndConsumption").val();
        if ($.trim(value).length) {
            if (parseFloat(value) >= 0) {
                if (purchaseAndProduction >= 0) {
                    $("#l_ClosingStock").text((closingStock - purchaseAndProduction) + parseFloat(value));
                } else {
                    $("#l_ClosingStock").text(closingStock + parseFloat(value));
                }
            }
        }
        else {
            if (purchaseAndProduction >= 0) {
                $("#l_ClosingStock").text(closingStock - purchaseAndProduction);
            } else {
                $("#l_ClosingStock").text(closingStock - 0);
            }
        }
    }
    $.validate({
        form: '#addEditPetrostock',
        validateOnBlur: false,
        modules: 'date',
        onError: function () {
            $("#status").text("");
            return false;
        },
        onSuccess: function ($form) {
            $("#status").text("");
            var submitButton = Ladda.create(document.querySelector('#submit-button'));
            submitButton.start();
            $("#submit-button").attr("disabled", true);
            var handler = $.post("<?php nav_url("petrostock/persist") ?>", $("#addEditPetrostock").serialize(), function (data) {
                submitButton.stop();
                try {
                    data = JSON.parse(data);
                    if (data.status) {
                        $("#status").html("<div  class='alert alert-block alert-success ajaxAlert'><p style='text-align: center'>" + data.message + "</p></div>");
                        var lastClosingBal = $("#l_ClosingStock").text();
                        $form.find('input:text').val('');
                        $("#l_ClosingStock").text(lastClosingBal);
                        $("#l_OpeningStock").text(lastClosingBal);
                        createAutoClosingAlert(".ajaxAlert", 2000);
                    } else {
                        $("#status").html("<div  class='alert alert-block alert-error alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><p style='text-align: center'>" + data.errorMessage + "</p></div>");
                    }
                } catch (err) {
                    $("#status").html("<div  class='alert alert-block alert-error alert-dismissable'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button><p style='text-align: center'>" + err + "<br>" + data + "</p></div>");
                    return false;
                }
                $("#submit-button").attr("disabled", false);
                submitButton.stop();
            })
                .error(function (error) {
                    console.log(error.responseText);
                    $("#submit-button").attr("disabled", false);
                    submitButton.stop();
                });

            return false;
        }
    });

    function createAutoClosingAlert(selector, delay) {
        var alert = $(selector).alert();
        window.setTimeout(function () {
            alert.alert('close')
        }, delay);
    }
</script>
</body>
</html>